﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Entity.OrganizationManage;

namespace YiSha.Service.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2024-12-17 01:27
    /// 描 述：服务类
    /// </summary>
    public class ModuleCategoryUserService :  RepositoryFactory
    {
        #region 获取数据
        public async Task<List<ModuleCategoryUserEntity>> GetList(ModuleCategoryUserListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<ModuleCategoryUser>> GetModuleCategoryUserList(ModuleCategoryUserListParam param)
        {
            var list = await this.BaseRepository().db.FindList<ModuleCategoryUser>(" select userId as Id from module_category_user GROUP BY userId ");

            foreach (var item in list)
            {
                var user=await this.BaseRepository().FindEntity<UserEntity>(item.Id.Value);

                item.UserName = user?.UserName;

                var data = this.BaseRepository().db.IQueryable<ModuleCategoryUserEntity>(t => t.userId == item.Id).ToList();

                if (data.Count>0)
                {
                    var data1 = this.BaseRepository().db.IQueryable<ModuleCategoryEntity>(t => data.Select(s=>s.module_category_Id).ToList().Contains(t.Id)).ToList();
                    item.moduleNames = string.Join(",", data1.Select(t => t.module_category_name));
                    item.moduleIds = string.Join(",", data1.Select(t => t.Id));
                }
            }

            return list.ToList();
        }

        public async Task<List<ModuleCategoryUserEntity>> GetPageList(ModuleCategoryUserListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list= await this.BaseRepository().FindList(expression, pagination);
            return list.ToList();
        }

        public async Task<ModuleCategoryUserEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<ModuleCategoryUserEntity>(id);
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(ModuleCategoryUserTree entity)
        {
            var db = await this.BaseRepository().BeginTrans();
            try
            {
                var data=db.IQueryable<ModuleCategoryUserEntity>(t => t.userId == entity.userId).ToList();

                if (data.Count>0)
                {
                    await db.Delete<ModuleCategoryUserEntity>(t => t.userId == entity.userId);
                }

                // 角色对应的菜单、页面和按钮权限
                if (!string.IsNullOrEmpty(entity.moduleIds))
                {
                    foreach (long menuId in TextHelper.SplitToArray<long>(entity.moduleIds, ','))
                    {
                        ModuleCategoryUserEntity moduleCategoryUser = new ModuleCategoryUserEntity();
                        moduleCategoryUser.Create();
                        moduleCategoryUser.userId = entity.userId;
                        moduleCategoryUser.Create();
                        moduleCategoryUser.module_category_Id = menuId;
                        await db.Insert(moduleCategoryUser);
                    }
                }
                await db.CommitTrans();
            }
            catch
            {
                await db.RollbackTrans();
                throw;
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<ModuleCategoryUserEntity>(t=> idArr.Contains(t.userId.Value));
        }
        #endregion

        #region 私有方法
        private Expression<Func<ModuleCategoryUserEntity, bool>> ListFilter(ModuleCategoryUserListParam param)
        {
            var expression = LinqExtensions.True<ModuleCategoryUserEntity>();
            if (param != null)
            {
            }
            return expression;
        }
        #endregion
    }
}
